﻿/**
 * @file guild_module_tool.hpp
 * @date 2021/06/29 10:06
 *
 * @author harrybao (harrybao@123u.com)
 *
 * @brief 公会模块工具
 *
 * @version v1.0.0
 *
 * @copyright Copyright © 2021 All Rights Reserved 上海欢乐互娱网络科技有限公司
 *
 * @par history:
 * <table>
 * <tr><th>Date                     <th>Version     <th>Author <th>Description
 * <tr><td>2021/06/29 10:06         <td>1.0.0       <td>harrybao
 * <td>创建初始版本
 * </table>
 */

#include <cinttypes>

#include "AnnounceData_struct.h"
#include "AnnounceMsg_struct.h"
#include "GuildModuleBrief_struct.h"
#include "GuildMember_struct.h"
#include "GuildModuleMembers_struct.h"
#include "guild_module_loader.hpp"

namespace guild_service {
namespace detail {

class GuildModuleTool {
 public:
  /**
   * @brief 计算公会福利
   *
   * @param service_name 服务名
   * @param guild_id 公会id
   * @param role_uuid 角色唯一id
   * @param welfare_award 公会奖励
   */
  static bool CalMemberWelfareAward(const std::string &service_name,
                                    const guild_id_type guild_id,
                                    const role_uuid_type role_uuid,
                                    uint64_t &welfare_award) {
    GuildModuleBrief module_brief;
    if (!detail::GetGuildModule<GuildModuleBrief>(service_name, guild_id,
                                            kGuildModuleBriefName, module_brief)) {
      return false;
    }
    GuildModuleMembers module_members;
    if (!detail::GetGuildModule<GuildModuleMembers>(
            service_name, guild_id, kGuildModuleMembersName, module_members)) {
      return false;
    }
    static double k1[6] = {0, 3.3, 2.2, 2.2, 1.7, 1.7};
    static int k2[6] = {0, 30, 45, 50, 55, 65};
    uint32_t guild_level = module_brief.level;
    if (guild_level > 5) {
      guild_level = 5;
    }
    std::size_t member_count = module_members.members.size();
    for (const auto &it : module_members.members) {
      if (it.role_uuid == role_uuid) {
        if (it.is_weekly_welfare_awarded) {
          return false;
        }
        int64_t x1 = 1ll * (k2[guild_level] * 60000 + 1200000);  //所有整数分子
        int64_t x2 = int64_t(member_count) * 350000 * 100;  //所有整数分母
        welfare_award = static_cast<int64_t>(x1 * k1[guild_level] * 0.2 /
                                             (x2 * k1[guild_level] * 1.0));
        return true;
      }
    }

    return false;
  }
};
}  // namespace detail
}  // namespace guild_service